import json

from requests_html import HTMLSession
from model.movieModel import MovieModel
from util.redisQueue import RedisQueue
import threading


class MovieSpider(threading.Thread):

    def __init__(self, threadID):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.session = HTMLSession()
        self.r = RedisQueue("movie_url")

    def run(self) -> None:
        self.openHtml()

    def openHtml(self):
        movie_url_info = self.r.get_wait()
        movie_url = json.loads(movie_url_info[-1])
        response = self.session.get(movie_url['url'])
        movie_items = response.html.find(".co_area2", first=True, _encoding='gb2312')
        title = movie_items.find(".title_all", first=True).text
        print(f'正在采集:{title}的信息')
        movie_simple_info = movie_items.find(".position", first=True).text
        movie_image_url = movie_items.find("#Zoom > p > img", first=True).attrs['src']
        movie_info = movie_items.find("#Zoom", first=True).text
        # print(f'movie_info:{movie_info}')
        movie_download_url = []
        for a in movie_items.find("#Zoom a"):
            href_url = str(a.attrs['href'])
            thunderhref_url = str(a.attrs['thunderhref'])
            if href_url.startswith('magnet'):
                movie_download_url.append(href_url)
            elif thunderhref_url.startswith('thunder'):
                movie_download_url.append(thunderhref_url)
            else:
                movie_download_url.append(a.text)
        movie = MovieModel(title, movie_simple_info, movie_image_url, movie_info, movie_download_url)
        print(f'{title}的信息采集成功啦！\n{movie}')


if __name__ == '__main__':
    MovieSpider().openHtml()